﻿using MyShop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 经典项目某某管理系统.Erp
{
    public partial class FrmGoodPutIn : Form
    {
        public FrmGoodPutIn()
        {
            InitializeComponent();
        }
        private void BindType()
        {
            DataTable dt = new DataTable();
            string sql = "select * from [Type]";
            DBHelper.PrepareSql(sql);
            dt = DBHelper.ExecQuery();
            DataRow dr = dt.NewRow();
            dr["TypeName"] = "请选择";
            dr["TypeID"] = "0";
            dt.Rows.InsertAt(dr, 0);
            this.cmbClass.DataSource = dt;
            this.cmbClass.ValueMember = "TypeID";
            this.cmbClass.DisplayMember = "TypeName";

        }

        private void ClearAllInputs(Control container)
        {
            foreach (Control ctrl in container.Controls)
            {
                if (ctrl is TextBox textBox)
                {
                    textBox.Clear(); // 或 textBox.Text = "";
                }
                else if (ctrl is ComboBox comboBox)
                {
                    if (comboBox.Items.Count > 0)
                        comboBox.SelectedIndex = 0; // 假设第0项是“请选择”
                    else
                        comboBox.SelectedIndex = -1;
                }
                // 如果有嵌套容器（如 Panel、GroupBox），递归处理
                else if (ctrl.HasChildren)
                {
                    ClearAllInputs(ctrl);
                }
            }
        }

        private void FrmGoodPutIn_Load(object sender, EventArgs e)
        {
            BindType();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select * from Goods where BarCode =@BarCode";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("BarCode", this.textBox1.Text);
            DataTable dt = DBHelper.ExecQuery();
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("没有找到该商品信息，请确认条形码是否正确！");
            }
            else
            {
                this.textBox2.Text = dt.Rows[0]["GoodsName"].ToString();
                this.cmbClass.SelectedIndex = Convert.ToInt32(dt.Rows[0]["TypeID"].ToString());
                this.textBox3.Text = dt.Rows[0]["StorePrice"].ToString();
                this.textBox4.Text = dt.Rows[0]["SalePrice"].ToString();
                this.textBox5.Text = dt.Rows[0]["Discount"].ToString();
                //this.textBox6.Text = dt.Rows[0]["StockNum"].ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "select * from Goods where BarCode =@BarCode";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("BarCode", this.textBox1.Text);
            DataTable dt = DBHelper.ExecQuery();
            if (dt.Rows.Count == 0)
            {
                //新增商品
                sql = "insert into Goods(BarCode,GoodsName,TypeID,StorePrice,SalePrice,Discount,StockNum,StockDate) " +
                    "values(@BarCode,@GoodsName,@TypeID,@StorePrice,@SalePrice,@Discount,@StockNum,@StockDate)";
                DBHelper.PrepareSql(sql);
                DBHelper.SetParameter("BarCode", this.textBox1.Text);
                DBHelper.SetParameter("GoodsName", this.textBox2.Text);
                DBHelper.SetParameter("TypeID", this.cmbClass.SelectedValue.ToString());
                DBHelper.SetParameter("StorePrice", this.textBox3.Text);
                DBHelper.SetParameter("SalePrice", this.textBox4.Text);
                DBHelper.SetParameter("Discount", this.textBox5.Text);
                DBHelper.SetParameter("StockNum", this.textBox6.Text);
                DBHelper.SetParameter("StockDate", DateTime.Now);
                int count = DBHelper.ExecNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("商品入库成功！");
                    ClearAllInputs(this);
                }
                else {
                    MessageBox.Show("商品入库失败！");
                }
            }
            else
            {
                //修改商品库存
                 sql = "update Goods set BarCode=@BarCode,GoodsName=@GoodsName," +
                    "TypeID=@TypeID,StorePrice=@StorePrice,SalePrice=@SalePrice," +
                    "Discount=@Discount,StockNum=StockNum+@StockNum," +
                    "StockDate=getdate() where BarCode=@BarCode";
                DBHelper.PrepareSql(sql);
                DBHelper.SetParameter("BarCode", this.textBox1.Text);
                DBHelper.SetParameter("GoodsName", this.textBox2.Text);
                DBHelper.SetParameter("TypeID", this.cmbClass.SelectedValue.ToString());
                DBHelper.SetParameter("StorePrice", this.textBox3.Text);
                DBHelper.SetParameter("SalePrice", this.textBox4.Text);
                DBHelper.SetParameter("Discount", this.textBox5.Text);
                DBHelper.SetParameter("StockNum", this.textBox6.Text);
                //DBHelper.SetParameter("StockDate", DateTime.Now);
                int count = DBHelper.ExecNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("商品更新成功！");
                    ClearAllInputs(this);
                }
                else
                {
                    MessageBox.Show("商品更新失败！");
                }
            }
        }
    }
}
